package max.ta.util.conf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;


// ---------------------------------------------------------
// Singleton to load system properties
// ---------------------------------------------------------
public final class ConfigurationLoader {


    private static final Logger logger = Logger.getLogger(ConfigurationLoader.class);
    // ------------------------------------------------------------------------------------------
    private static final File PROPERTIES_FILE = new File("conf/configuration.properties");
    // ------------------------------------------------------------------------------------------
    private static ConfigurationLoader INSTANCE;


    private final Properties properties = new Properties();


    public synchronized String getProperty(String key) {
	return properties.getProperty(key);
    }


    public synchronized String getProperty(String key, String defaultValue) {
	return properties.getProperty(key, defaultValue);
    }


    public synchronized static ConfigurationLoader getInstance() {
	if (INSTANCE == null) {
	    INSTANCE = new ConfigurationLoader();
	}
	return INSTANCE;
    }

    private ConfigurationLoader() {
	super();
	try {
	    logger.info("############################################################################");
	    logger.info("Load configuration properties from : " + PROPERTIES_FILE.getAbsolutePath());
	    logger.info("############################################################################");
	    properties.load(new FileInputStream(PROPERTIES_FILE));
	    for (Object key : properties.keySet()) {
		logger.info(" *** " + key + " : " + properties.getProperty((String) key));
	    }
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	    throw new ConfigurationException(e);
	} catch (IOException e) {
	    e.printStackTrace();
	    throw new ConfigurationException(e);
	}
    }




}
